<?php


namespace Apps\Manage\Controllers;


use Apps\Common\Models\BeautyParlorComment;
use PhalconPlus\Common\ResponseResult;
use PhalconPlus\Common\ResponseResultStatus;
use Apps\Common\Models\UserBase;
/**
 * 美容院评论管理
 * @author     chenxiaolin
 * @date        2016年1月19日
 * @version     2.0.0
 * @copyright   Copyright 2016 meelier.com
 */
class BpcommentController extends BpBaseController
{
    public function initialize()
    {
        parent::initialize();
        $this->setMainNav('bp');
    }

    /**
     * 美容院评论列表
     * @date: 2016年1月19日 
     * @author: chenxiaolin
     */
    public function listAction()
    {
        $this->setLeftNav('comment_list');
        $req = $this->request;
        $id = intval($req->getQuery('id', null, 0));
        if($id <= 0) {
            $this->view->disable();
            echo '美容院不存在！';
            return;
        }
        $page = intval($req->getQuery('page', null, 1));
        $page = $page > 0 ? $page : 1;
        $limit = $this->pageNavLimit;
        $offset = $limit * ($page - 1);
        
        $user_name = $req -> getQuery('user_name');
        $comment_addtime = $req->getQuery('comment_addtime');
        $timeSlotarr=explode("至",$comment_addtime);
        
        
        $where = []; 
        if($user_name != ''){
            $user = UserBase::query()
            -> columns(['user_id'])
            -> where("user_nickname like '%".$user_name."%'")
            -> execute();
                foreach ($user as $k => $v){
                    $user_id .= $v['user_id'].',';
                }
                $user_id = rtrim($user_id, ",");
                if(!empty($user_id)){
                    $where[]="Apps\Common\Models\BeautyParlorComment.user_id in ( $user_id )";
                }else{
                    $where[]="Apps\Common\Models\BeautyParlorComment.user_id in ( 0 )";
                }
                $this->view->setVar("user_name",$user_name);
        }
         
        if($comment_addtime != ''){
            $where[]="comment_addtime > '".$timeSlotarr[0]."'";
            $where[]="comment_addtime < '".$timeSlotarr[1]."'";
            $this->view->setVar("comment_addtime",$comment_addtime);
        }

        $where[] = "beauty_parlor_id = $id";
         
        $whereStr = implode ( ' AND ', $where );
        
        
        $list = BeautyParlorComment::query()
            ->columns([
                'comment_id',
                'ub.user_id',
                'ub.user_nickname',
                'ub.user_cover',
                'ub.user_gender',
                'comment_info',
                'comment_photos',
                'comment_state',
                'comment_addtime',
                'comment_reply',
                'comment_reply_addtime'
            ])
            ->leftJoin('Apps\Common\Models\UserBase', 'ub.user_id = Apps\Common\Models\BeautyParlorComment.user_id', 'ub')
            //->where('beauty_parlor_id = :id:', ['id' => $id])
            ->where($whereStr)
            ->limit($limit, $offset)
            ->orderBy('comment_id DESC')
            ->execute()->toArray();
        foreach ($list as $k => $v){
            if (unserialize(base64_decode($v['comment_info']))){
                $list[$k]['comment_info'] = unserialize(base64_decode($v['comment_info']));
            }
        }
        
        //$total = BeautyParlorComment::count('beauty_parlor_id = ' . $id);
        $total = BeautyParlorComment::count($whereStr);
        $this->view->setVar('commentList', $list);
        $this->view->setVar('total', $total);
        $this->view->setVar('page', $page);
        $this->view->setVar('limit', $limit);
        $this->view->setVar('id', $id);
    }

    /**
     * 删除\恢复 美容院评论
     * @date: 2016年1月19日 
     * @author: chenxiaolin
     */
    public function setStateAction()
    {
        $req = $this->request;
        if($req->isPost()) {
            $id = intval($req->getPost('id', null, 0));
            $state = intval($req->getPost('state', null, -10000));
            if(!in_array($state, [-1, 0, 1])) {
                return (new ResponseResult())->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '请求异常');
            }
            $info = BeautyParlorComment::findFirst('comment_id=' . $id);
            if($info) {
                $info->update(['comment_state' => $state]);
            }
            return (new ResponseResult())->sendResult('ok');
        } else {
            return (new ResponseResult())->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '请求异常');
        }
    }
}